package com.jeff.android.lab.module.anim.loading;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.jeff.android.lab.R;

/**
 * @author: dingfeng
 * @date: 2020-06-05
 * @since: 1.0.0
 **/
public class LoadingDialog extends Dialog {

    private Context mContext;

    private TextView mTextViewLoadingMsg;

    public LoadingDialog(@NonNull Context context) {
        this(context, R.style.LoadingDialogTheme);
    }

    public LoadingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
    }

    protected LoadingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        this(context, R.style.LoadingDialogTheme);
        setCancelable(cancelable);
        setOnCancelListener(cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widget_dialog_loading);

        mTextViewLoadingMsg = findViewById(R.id.tv_loading_msg);
    }

    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();

        if (mContext != null &&
                mContext instanceof Activity && !((Activity) mContext).isFinishing()) {
            if (!((Activity) mContext).isFinishing())
                super.dismiss();
        }
    }

    public void setLoadingText(String msg) {
        if (!TextUtils.isEmpty(msg) && mTextViewLoadingMsg != null) {
            mTextViewLoadingMsg.setText(msg);
        }
    }

}
